$(function(){
	
	$(".possible_you_like .possibleLike_pro .pro").mouseover(function(){
		$(this).children(" span:nth-child(4)").show()
	}).mouseout(function(){
		$(this).children(" span:nth-child(4)").hide()
	})
	
	
	var  pro = getCookie("pro")
	console.log(pro)
	if(pro){
		var pro = JSON.parse(pro)
		for(i=0;i<pro.data.length;i++){//遍历购物车
			html = ""
			html = "<tr><td><input type='checkbox' name='sel_this' id='sel_this' value='' checked = 'checked' /></td>"
			html +="<td><img src='" + pro.data[i].img +"'/></td>"
			html +="<td>" + pro.data[i].name +"</td>"
			html +="<td>"
			html +="<button class='sub'>-</button>"
			html +="<input type='text' name='mount'  class='mount' value='1'  />"
			html +="<button class='add'>+</button>"
			html +="</td>"
			html +="<td class = 'proPrice'>"+ pro.data[i].price +"</td>"
			html += "<td>288</td>"
			html += "<td><span class = 'del'>删除</span>|<a href='#'>收藏</a></td></tr>"
			$("#cart_pro").append(html)	
		}
	}else{
		
		$("#cart_pro").append("<tr><td colspan='7'>购物车为空</td></tr>")	
	}
	
	
	
	//增减数量
	$(".add").click(function(){
		var mount =parseInt( $(this).closest("td").children(".mount").val())
		$(this).closest("td").children(".mount").val((mount+1))
		totalPrice()
	})
	$(".sub").click(function(){
		var mount =parseInt( $(this).closest("td").children(".mount").val())
		if(mount<=1){
			alert("最少购买数不为零")
		}else{
			$(this).closest("td").children(".mount").val(mount-1)
			totalPrice()
		}
		
	})
	totalPrice()
	function totalPrice(){//算总价
		var total = 0;
		$(".mount").each(function(){
			var mount =parseInt($(this).val())
			var tr = $(this).closest("tr").children(".proPrice").html()
			var	price = mount*tr
				total += price;
		})
		$("#total_price").html( total )
	}
	
	//删除购物车
	$("#cart_pro td .del").click(function(){
		console.log( JSON.parse(getCookie("pro")) )
		var index = $(this).closest("tr").index() 
		    pro.data.splice((index-1),1)
		var obj = JSON.stringify(pro)
			var d = new Date();
			d.setDate(d.getDate()+10);
			setCookie("pro",obj,d)
			history.go(0)
		console.log( JSON.parse(getCookie("pro")) )
	})
	//清空购物车
	$("#clear_car").click(function(){
		if(window.confirm("你确定要清空购物车吗？")){
			var d = new Date();
			removeCookie("pro",d)
			history.go(0)
		}else{
			return false;
		}
	})
	
	
	//猜你喜欢商品添加购物车
		$(".pro .add_cart").click(function(){
			//console.log($(this).closest(".pro").children('img').attr("src"))
				var pro = getCookie("pro");
				var	obj0 = {
						"name":$('.name').html(),
						"img":$(this).closest(".pro").children('img').attr("src"),
						"price":$(".price").html() ,
						"comment":0
					}
				if(pro){ //购物车不为空
					var  pro = JSON.parse(getCookie("pro"))
					var d = new Date();
					d.setDate(d.getDate()+10);
					pro.data.push(obj0)
					cart_pro = JSON.stringify(pro)
					setCookie("pro",cart_pro,d)
					console.log(JSON.parse(getCookie("pro")))
				}else{  //购物车是空
					var pro = {data:[]}
					var d = new Date();
					d.setDate(d.getDate()+10);
					pro.data.push(obj0)
					cart_pro = JSON.stringify(pro)
					setCookie("pro",cart_pro,d)
					console.log(cart_pro)
				}
					
				$("#add_cart").show() //重新获取cookie
				var pro = getCookie("pro");
				var pro = JSON.parse(getCookie("pro"))
				$("#cart_mount").html(pro.data.length) //显示购物车数量
				var total_price = 0;
				for(var j=0;j<pro.data.length;j++){
					total_price +=parseInt( pro.data[j].price )
				}
				$("#price").html(total_price)
				history.go(0)
		})//
			$("#close").click(function(){
				$("#add_cart").hide()
			})
	
})
